20. Quiz: Practice Debugging

The following quiz questions focus on handling common errors or exceptions that you may encounter while executing your program.

QUIZ QUESTION::

The following are some common exceptions. Again, "exceptions" are errors detected during execution. See if you can recall and match each error with its meaning and the steps you will take to handle the exception. If you need help, feel free to do some research online to learn more about the exception.

ANSWER CHOICES:



Example Exception

How would you try to handle the exception?

NameError: name 'abc_dict' is not defined

UnboundLocalError

ValueError: too many values to unpack (expected 2)

TypeError: unsupported operand type(s) for +: 'int' and 'str'

SOLUTION:

Example Exception

How would you try to handle the exception?

ValueError: too many values to unpack (expected 2)

UnboundLocalError

TypeError: unsupported operand type(s) for +: 'int' and 'str'

NameError: name 'abc_dict' is not defined

Quiz: Practice Debugging

In the workspace at the bottom of the page, there is a piece of code in the ** user_input_numlist.py** Python file. The code prompts the user to enter 10 two-digit numbers. It should then find and print the sum of all of the even numbers among those that were entered.

But there is a bug in the code! When I input a number, I get the following TypeError. Use the programming environment provided below with a Terminal and code editor to debug the code.

** Using the workspace:**

Here are a few tips orienting you to this kind of workspace.

In the top panel is a code editor where you can edit the Python file ** user_input_numlist.py**. Scroll up and down in this panel to see all the code. You can also expand or shrink this panel by clicking and dragging its bottom border.

In the bottom panel, you can execute your Python file by clicking on New Terminal and entering ** python user_input_numlist.py** on the command line.

The solutions have been provided on the next page, but I encourage you to try figuring out where the bug is in the code, and fixing it yourself.

** Sample Output:**
This is what the output should look like.

>>> user_list: [23, 24, 25, 26, 27, 28, 29, 30, 31, 22]
>>> The sum of the even numbers in user_list is: 130.

Code

If you need a code on the https://github.com/udacity.

  • userCode:

    python user_input_numlist.py